package com.zbkj.admin.controller.platform;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zbkj.common.model.Warehouse;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.WarehouseSearchRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 仓库表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/warehouse")
@Api(tags = "仓库表")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    /**
     * 分页显示仓库表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author keNan
     * @since 2025-06-05
     */
    @PreAuthorize("hasAuthority('platform:warehouse:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Warehouse>> getList(@Validated WarehouseSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<Warehouse> warehouseCommonPage = CommonPage.restPage(warehouseService.getList(request, pageParamRequest));
        return CommonResult.success(warehouseCommonPage);
    }

    /**
     * 新增仓库表
     *
     * @param warehouse 新增参数
     * @author keNan
     * @since 2025-06-05
     */
    @PreAuthorize("hasAuthority('platform:warehouse:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated Warehouse warehouse) {
        Boolean b = warehouseService.addWarehouse(warehouse);
        if (b) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除仓库表
     *
     * @param id Integer
     * @author keNan
     * @since 2025-06-05
     */
    @PreAuthorize("hasAuthority('platform:warehouse:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        LambdaUpdateWrapper<Warehouse> luw = new LambdaUpdateWrapper<>();
        luw.set(Warehouse::getIsDel, 1);
        luw.eq(Warehouse::getId, id);
        if (warehouseService.update(luw)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改仓库表
     *
     * @param warehouse 修改参数
     * @author keNan
     * @since 2025-06-05
     */
    @PreAuthorize("hasAuthority('platform:warehouse:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated Warehouse warehouse) {
        if (null == warehouse.getId()) {
            return CommonResult.failed("id不能为空");
        }
        if (1 == warehouse.getIsDefault()) {
            LambdaUpdateWrapper<Warehouse> luw = new LambdaUpdateWrapper<>();
            luw.set(Warehouse::getIsDefault, 0);
            luw.eq(Warehouse::getIsDefault, 1);
            warehouseService.update(luw);
        }
        return warehouseService.updateById(warehouse) == true ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 查询仓库表信息
     *
     * @param id Integer
     * @author keNan
     * @since 2025-06-05
     */
    @PreAuthorize("hasAuthority('platform:warehouse:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<Warehouse> info(@RequestParam(value = "id") Integer id) {
        Warehouse warehouse = warehouseService.getById(id);
        return CommonResult.success(warehouse);
    }


}



